<?php namespace App\Services;

use App\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;

class Registrar implements RegistrarContract
{

  /**
   * Get a validator for an incoming registration request.
   *
   * @param  array $data
   * @return \Illuminate\Contracts\Validation\Validator
   */
  public function validator(array $data)
  {
    $rules = [
        'name' => 'required|min:3|max:32|unique:users',
        'password' => 'required|confirmed|min:6|max:32',
    ];

    $messages = [
        'required' => ':attribute 没有填写.',
        'unique' => ':attribute 已经被其他用户使用.',
        'min' => ':attribute 必须超过:min个字符',
        'max' => ':attribute 长度只允许在:max个内',
        'confirmed' => '确认 :attribute 不一致',
    ];

    return Validator::make($data, $rules, $messages);
  }

  /**
   * Create a new user instance after a valid registration.
   *
   * @param  array $data
   * @return User
   */
  public function create(array $data)
  {
    return User::create([
        'name' => $data['name'],
        'password' => bcrypt($data['password']),
    ]);
  }

}
